package cn.iocoder.yudao.module.els.controller.admin.codeGenerator;

import lombok.Data;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.els.controller.admin.codeGenerator.vo.*;
import cn.iocoder.yudao.module.els.dal.dataobject.codeGenerator.CodeGeneratorDO;
import cn.iocoder.yudao.module.els.dal.dataobject.codeGeneratorDetail.CodeGeneratorDetailDO;
import cn.iocoder.yudao.module.els.service.codeGenerator.CodeGeneratorService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 编码生成器")
@RestController
@RequestMapping("/els/code-generator")
@Validated
public class CodeGeneratorController {

    @Resource
    private CodeGeneratorService codeGeneratorService;

    @GetMapping("getNextCode")
    public CommonResult<String> getNextCode(@RequestParam("codeType") String codeType) throws InterruptedException{
        return success(codeGeneratorService.getNextCode(codeType,null));
    }

    @GetMapping("getNextCodes")
    public CommonResult<List<String>> getNextCodes(@RequestParam("codeType") String codeType,@RequestParam("size") int size) throws InterruptedException{
        return success(codeGeneratorService.getNextCodes(codeType,null,size));
    }

    @GetMapping("getNextCodeVar")
    public CommonResult<String> getNextCodeVar(@RequestParam("codeType") String codeType) throws InterruptedException{
        UserInfo u = new UserInfo();
        u.setUsername("张三").setSex("01").setAge("024");
        return success(codeGeneratorService.getNextCode(codeType,u));
    }

    @Data
    private static class UserInfo{
        private String username;
        private String sex;
        private String age;
    }

    @PostMapping("/create")
    @Operation(summary = "创建编码生成器")
    @PreAuthorize("@ss.hasPermission('els:code-generator:create')")
    public CommonResult<Long> createCodeGenerator(@Valid @RequestBody CodeGeneratorSaveReqVO createReqVO) {
        return success(codeGeneratorService.createCodeGenerator(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码生成器")
    @PreAuthorize("@ss.hasPermission('els:code-generator:update')")
    public CommonResult<Boolean> updateCodeGenerator(@Valid @RequestBody CodeGeneratorSaveReqVO updateReqVO) {
        codeGeneratorService.updateCodeGenerator(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码生成器")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('els:code-generator:delete')")
    public CommonResult<Boolean> deleteCodeGenerator(@RequestParam("id") Long id) {
        codeGeneratorService.deleteCodeGenerator(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码生成器")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('els:code-generator:query')")
    public CommonResult<CodeGeneratorRespVO> getCodeGenerator(@RequestParam("id") Long id) {
        CodeGeneratorDO codeGenerator = codeGeneratorService.getCodeGenerator(id);
        return success(BeanUtils.toBean(codeGenerator, CodeGeneratorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码生成器分页")
    @PreAuthorize("@ss.hasPermission('els:code-generator:query')")
    public CommonResult<PageResult<CodeGeneratorRespVO>> getCodeGeneratorPage(@Valid CodeGeneratorPageReqVO pageReqVO) {
        PageResult<CodeGeneratorDO> pageResult = codeGeneratorService.getCodeGeneratorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CodeGeneratorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码生成器 Excel")
    @PreAuthorize("@ss.hasPermission('els:code-generator:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCodeGeneratorExcel(@Valid CodeGeneratorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CodeGeneratorDO> list = codeGeneratorService.getCodeGeneratorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码生成器.xls", "数据", CodeGeneratorRespVO.class,
                        BeanUtils.toBean(list, CodeGeneratorRespVO.class));
    }

    // ==================== 子表（编码生成器明细） ====================

    @GetMapping("/code-generator-detail/list-by-generator-id")
    @Operation(summary = "获得编码生成器明细列表")
    @Parameter(name = "generatorId", description = "id")
    @PreAuthorize("@ss.hasPermission('els:code-generator:query')")
    public CommonResult<List<CodeGeneratorDetailDO>> getCodeGeneratorDetailListByGeneratorId(@RequestParam("generatorId") Long generatorId) {
        return success(codeGeneratorService.getCodeGeneratorDetailListByGeneratorId(generatorId));
    }

}